export default {
  namespaced: true,
  state() {
    return {
      // 1. 收货地址
      address: JSON.parse(uni.getStorageSync("address") || "{}"),
      // 2. token
      token: uni.getStorageSync("token") || "",
      // 3. 用户信息
      userInfo: JSON.parse(uni.getStorageSync("userInfo") || "{}"),
      // 4. 登录成功后回跳的对象信息
      redirectInfo: null,
    };
  },
  getters: {
    fullAddress(state) {
      const { provinceName, cityName, countyName, detailInfo } = state.address;
      // 判断: 只要没有内容, 则直接返回空字符串
      if (!provinceName || !cityName || !countyName || !detailInfo) return "";
      return `${provinceName}${cityName}${countyName}${detailInfo}`;
    },
  },
  mutations: {
    // 1. 存储收货地址
    setAddress(state, address) {
      state.address = address;
      // 把地址对象存储到本地
      uni.setStorageSync("address", JSON.stringify(state.address));
    },
    // 2. 存储用户信息
    setUserInfo(state, userInfo) {
      state.userInfo = userInfo;
      uni.setStorageSync("userInfo", JSON.stringify(state.userInfo));
    },
    // 3. 存储token
    setUserToken(state, token) {
      state.token = token || "";
      uni.setStorageSync("token", state.token);
    },
    // 4. 存储回跳信息
    // {url: '', type: 'tab'} tab  navigate
    setRedirectInfo(state, redirectInfo) {
      state.redirectInfo = redirectInfo;
    },
  },
  actions: {},
};
